package com.yyx.jupiter.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtils {
    public static String getNowTime(String format) {
        return new SimpleDateFormat(format).format(new Date());
    }

    public static String dateAdd(String date, Integer span, String format) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new SimpleDateFormat(format).parse(date));
        calendar.add(Calendar.DATE, span);
        return new SimpleDateFormat(format).format(calendar.getTime());
    }

    public static Integer dateDiff(String dateStr1, String dateStr2) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = dateFormat.parse(dateStr1);
        Date date2 = dateFormat.parse(dateStr2);
        long diff = (date2.getTime() - date1.getTime()) / 1000 / 60 / 60 / 24;
        return (int) diff;
    }
}
